<?php


namespace app\admin\controller;


use app\admin\service\RequestService;
use app\model\Ad;
use app\model\AdType;


class AdController extends AController{

    public function index(RequestService $curdService){
        $list = $curdService->index(new Ad(), ['adType'])->each(function ($v){
            $v->typeTitle = getRelValue($v->adType, 'title');
        });

        return jsonSuccess([
            'items' => $list->items(),
            'total' => $list->total()
        ]);

    }

    public function add(RequestService $curdService){
        return $curdService->add(new Ad());
    }

    public function edit(RequestService $curdService){
        if (request()->isPost()){
            return $curdService->edit(new Ad());
        }else{
            $id = request()->param('id');
            $info = Ad::find($id);
            return jsonSuccess([
                'info' => $info
            ]);
        }
    }

    public function del(RequestService $curdService){
        return $curdService->del(new Ad());
    }

}
